package com.wontube.smr.bo.constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.dao.bo.GenreSelectDao;


/**
 * @author tanggf(2012-9-3)
 *
 */
public class MovieGenreConstants {
	private static Map<String, List<String>> multiGenresMap = null;	
	private static List<Genre> displayGenres = null;
	
	static {		
		loadMovieGenres();
	}
	
	private static void loadMovieGenres(){
		//首先处理多genre问题
		multiGenresMap = GenreSelectDao.selectMultiGenres(Genre.CATE_MOVIE);	
		
		Set<Genre> sorts = new TreeSet<Genre>();		
		//收集展示genres
		List<Genre> genres = GenreSelectDao.selectGenres(Genre.CATE_MOVIE);
		for (Genre genre : genres) {
			if( ! multiGenresMap.containsKey(genre.getDisplayName())){
				sorts.add(genre);
			}
		}
		
		//增加multi genre
		if(multiGenresMap != null){
			Set<String> keyGenres = multiGenresMap.keySet();
			for (String name : keyGenres) {
				sorts.add(new Genre(name, name));
			}			
		}
		
		displayGenres = new ArrayList<Genre>(sorts);
	}
	
	public static List<Genre> getAllMovieGenres(){
		return displayGenres;
	}
	
	/**
	 * 拼接一对多的genres
	 * @param genreName
	 * @return
	 */
	public static String joinQueryGenreNames(String genreName){
		if(multiGenresMap == null){
			return genreName;
		}
		
		List<String> all = multiGenresMap.get(genreName);
		if(all != null){
			return ArrayUtils.join(all, ",");
		}
		return genreName;
	}
}
